package com.example.service;

import com.example.entity.Attachment;
import com.example.entity.IncomingDoc;
import com.example.exception.CustomException;
import com.example.mapper.AttachmentMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AttachmentService {

    @Resource
    private AttachmentMapper attachmentMapper;

    public void save(Attachment attachment) {
        attachmentMapper.insert(attachment);
    }

    public Attachment getWithOpinions(Integer id){
        Attachment attachment = attachmentMapper.selectById(id);
        if (attachment == null){
            throw new CustomException("5016", "收文不存在");
        }
        return attachment;
    }

    public PageInfo<Attachment> selectPage(Attachment attachment, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize); // 启动分页
        List<Attachment> attachments = attachmentMapper.selectPageList(attachment);
        return PageInfo.of(attachments);
    }

    public void deleteById(Integer id) {
        attachmentMapper.deleteById(id);
    }

    public void deleteBatch(List<Integer> ids) {
        attachmentMapper.deleteBatch(ids);
    }

    public void updateById(Attachment attachment) {
        attachmentMapper.updateById(attachment);
    }

    public List<Attachment> preview(Attachment attachment) {
        return attachmentMapper.preview(attachment);
    }
}
